package com.zz.bsea.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zz.bsea.entity.Product;
import com.zz.bsea.service.ProductService;

/**
 * Servlet implementation class ProductServlet
 */
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String pname=request.getParameter("pname");
		 String pcate=request.getParameter("pcate");
		 String pprice=request.getParameter("pprice");
		 String pcreateby=request.getParameter("pcreateby");
		 Double dprice=Double.parseDouble(pprice);
		 Product p=new Product();
		 p.setCategory(pcate);
		 p.setName(pname);
		 p.setPrice(dprice);
		 p.setCreatedby(pcreateby);
		 ProductService us=new ProductService();
		 response.setCharacterEncoding("utf-8");
		 response.setContentType("text/html;charset=utf-8");
		 if(us.add(p)) {
				//重定向
			 response.sendRedirect("/java11/productList.jsp");
				 //请求转发
//			 request.getRequestDispatcher("/java11/BseaLogin.jsp").forward(req, resp);
			 }else {
				 response.sendRedirect("/java11/productList.jsp");
			 }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
